package com.cyy.demo.service.impl;

import com.cyy.demo.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {

    @Async("asyncServiceExecutor")
    @Override
    public void async() {
        log.info("async service start");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("async service end");
    }


    @Async("asyncServiceExecutor")
    @Override
    public Future<String> async2() {
        log.info("async service start");
        Future<String> future;
        try {
            Thread.sleep(2000);
            future = new AsyncResult<String>("success");

        } catch (InterruptedException e) {
            future = new AsyncResult<String>("error");
            log.error("接口调用失败,{}",e);
        }
        log.info("async service end");
        return future;
    }

    @Override
    public void sync() {
        log.info("sync service start");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("sync service end");
    }
}
